<template>
  <div>
    <div>
      <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="2"
        :formType="type"
        :isSubmitBtn="false"
        labelWidth="100"
        @formChange="formChangeHandle"
      />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button v-if="type !== 'show'" @click="saveDialog" type="primary">保 存</el-button>
    </div>

    <BaseDialog
      :visible.sync="isShow"
      :title="'整车分类列表'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <TopFilter
      :list="topFilterList"
      :cols="2"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
    ></TopFilter>

    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      selectionType="single"
      :onRowSelectChange="onRowSelectChange"
      :datakey="'records'"
      :onColumnsChange="columns => this.columns = columns"
    >
    </FilterTable>
    <div :style="{position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'}">
      <el-button size="small" @click="cancle" type="primary">取消</el-button>
      <el-button size="small" @click="addHandel" type="">确定</el-button>
    </div>
    </BaseDialog>
  </div>
</template>

<script>
import {
  getCar,
  getBrandList,
  getSeriesList,
  getModelList,
  getPackageList,
  getWcolorList,
  getIcolorList,
  addCar,
  updateCar,
  getCarExport,
  importCar,
  getExcelList,
  editCarPrice,
  qrySortDefinitionPage
 } from '@/api/common/cars/masterDataCars';
import { dictionary } from '@/utils/dictMixin';
import { numberCheck,numberCheckMax100} from '@/utils/validate'

export default {
  name: 'SeriesModal',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    productId: String|Number,
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList(),
      topFilterList: this.createTopFilters(),
      isShow:false,
      fetchApi: qrySortDefinitionPage,
      fetchParams:{},
      columns: this.createTableColumns(),
      handleType: '',
      formData: {},
      tableData:[],
    };
  },
  created() {
    this.init();
    // this.getDictBrandList()
  },
  methods: {
    createTopFilters() {
      return [
        {
          type: 'INPUT',
          label: '关键字',
          fieldName: 'productCode',
          placeholder: '请输入',
          style: { width: '200px' },
        }
      ];
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '产品代码',
          disabled:true,
          fieldName: 'productCode',
          placeholder: '产品代码'
        },
        {
          type: 'INPUT',
          label: '产品名称',
          disabled:true,
          fieldName: 'productName',
          placeholder: '产品名称'
        },
        // {
        //   type: 'INPUT',
        //   label: '分类代码',
        //   disabled:true,
        //   fieldName: 'baseCode',
        //   placeholder: '产品代码',
        //   initialValue: '',
        //   unitRender: () => {
        //     return <el-button slot="append" icon="el-icon-search" onClick={this.openDriveCancel}></el-button>;
        //   },
        //
        // },
        // {
        //   type: 'INPUT',
        //   label: '分类名称',
        //   disabled:true,
        //   fieldName: 'baseName',
        //   placeholder: '产品名称',
        //   initialValue: '',
        // },

        {
          type: 'SELECT',
          label: '品牌',
          disabled:true,
          fieldName: 'brandName',
          placeholder: '品牌',
          itemList: [],
          // change: val => {
          //   this.changeBrandSelect(val);
          // }
        },
        {
          type: 'SELECT',
          label: '车系',
          disabled:true,
          fieldName: 'seriesName',
          placeholder: '车系',
          itemList: [],
          // change: val => {
          //   this.changeSeriesSelect(val);
          // }
        },
        {
          type: 'INPUT',
          label: '车型代码',
          disabled:true,
          fieldName: 'modelCode',
          placeholder: '车型代码',
        },
        {
          type: 'SELECT',
          label: '车型名称',
          disabled:true,
          fieldName: 'modelName',
          placeholder: '车型名称',
          itemList: [],
          // change: val => {
          //   this.changeModelSelect(val);
          // }
        },
        {
          type: 'INPUT',
          label: '配置代码',
          disabled:true,
          fieldName: 'configCode',
          placeholder: '配置代码',
        },
        {
          type: 'SELECT',
          label: '配置名称',
          disabled:true,
          fieldName: 'packageName',
          placeholder: '配置名称',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '选装代码',
          disabled:true,
          fieldName: 'optionCode',
          placeholder: '车型名称',
        },
        {
          type: 'SELECT',
          label: '选装名称',
          disabled:true,
          fieldName: 'optionName',
          placeholder: '选装名称',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '外饰代码',
          disabled:true,
          fieldName: 'colorCode',
          placeholder: '外饰代码',
        },
        {
          type: 'INPUT',
          label: '外饰名称',
          disabled:true,
          fieldName: 'color',
          placeholder: '外饰名称',
        },
        {
          type: 'INPUT',
          label: '内饰代码',
          disabled:true,
          fieldName: 'trimColorCode',
          placeholder: '内饰色名称',
        },
        {
          type: 'INPUT',
          label: '内饰名称',
          disabled:true,
          fieldName: 'trimColor',
          placeholder: '内饰名称',
        },
        {
          type: 'SELECT',
          label: '产品状态',
          fieldName: 'productStatus',
          disabled:true,
          placeholder: '产品状态',
          itemList: this.createDictList('1408')
        },
        {
          type: 'DATE',
          label: '上市日期',
          disabled:true,
          fieldName: 'enterDate',
          placeholder: '上市日期'
        },
        {
          type: 'DATE',
          label: '退市日期',
          disabled:true,
          fieldName: 'exeuntDate',
          placeholder: '退市日期'
        },

        // {
        //   type: 'INPUT',
        //   label: '储备高限',
        //   disabled:true,
        //   fieldName: 'maxReserve',
        //   maxlength:8,
        //   placeholder: '储备高限',
        //   rules: [{ required: false,  trigger: 'blur',validator: numberCheck }]
        // },
        // {
        //   type: 'INPUT',
        //   label: '储备低限',
        //   disabled:true,
        //   maxlength:8,
        //   fieldName: 'minReserve',
        //   placeholder: '储备低限',
        //   rules: [{ required: false,  trigger: 'blur',validator: numberCheck }]
        // },
        {
          type: 'INPUT',
          label: '车厂指导价',
          disabled:true,
          fieldName: 'oemDirectivePrice',
          placeholder: '车场指导价'
        },
        // {
        //   type: 'INPUT',
        //   label: '采购单价',
        //   disabled:true,
        //   fieldName: 'purchasePrice',
        //   placeholder: '采购单价',
        // },
        // {
        //   type: 'INPUT',
        //   label: '整车加价率',
        //   fieldName: 'vehicleMarkupRate',
        //   maxlength:3,
        //   initialValue:"",
        //   placeholder: '整车加价率',
        //   onInput:value=>{
        //     let purchasePrice = this.formList.find(x=>x.fieldName == "purchasePrice").initialValue
        //     // console.log(value,"val",purchasePrice)
        //     if(100 >= value > 0 ){
        //       if(purchasePrice > 0){
        //         this.formList.find(x=>x.fieldName == "vehiclePrice").initialValue =Math.round(purchasePrice * (1+value/100)*100)/100
        //       }
        //     }else{
        //       this.$message(`整车加价率在0-100之间`);
        //     }
        //   },
        //   rules: [{ required: false,  trigger: 'blur',validator:numberCheckMax100 }]
        // },
        // {
        //   type: 'INPUT',
        //   label: '销售指导价',
        //   fieldName: 'vehiclePrice',
        //   initialValue:"",
        //   maxlength:16,
        //   placeholder: '销售指导价',
        //   rules: [{ required: false,  trigger: 'blur',validator: numberCheck }]
        // },
        // {
        //   type: 'INPUT',
        //   label: '销售底价',
        //   maxlength:16,
        //   fieldName: 'salesBasePrice',
        //   placeholder: '销售底价',
        //   rules: [{ required: false,  trigger: 'blur',validator: numberCheck }]
        // },
        // {
        //   type: 'INPUT',
        //   label: '备注',
        //   disabled:true,
        //   fieldName: 'remark',
        //   placeholder: '备注'
        // },
        {
          type: 'INPUT',
          label: '项目代号',
          disabled:true,
          fieldName: 'projectCode',
          placeholder: '项目代号'
        },
        {
          type: 'INPUT',
          label: '版本号',
          disabled:true,
          fieldName: 'salesVersionName',
          placeholder: '版本号'
        },
        {
          type: 'INPUT',
          label: '年型',
          disabled:true,
          fieldName: 'yearModelName',
          placeholder: '年型'
        },
        {
          type: 'INPUT',
          label: '动力电池编码',
          disabled:true,
          fieldName: 'batteryCode',
          placeholder: '动力电池编码'
        },
        {
          type: 'INPUT',
          label: '动力电池',
          disabled:true,
          fieldName: 'batteryVolume',
          placeholder: '动力电池'
        },
        {
          type: 'INPUT',
          label: '动力电池品牌',
          disabled:true,
          fieldName: 'batteryOem',
          placeholder: '动力电池品牌'
        },
        {
          type: 'INPUT',
          label: '电机编码',
          disabled:true,
          fieldName: 'powerCode',
          placeholder: '电机编码'
        },
        {
          type: 'INPUT',
          label: '电机',
          disabled:true,
          fieldName: 'powerCodeDesc',
          placeholder: '电机'
        },
        {
          type: 'INPUT',
          label: '电机品牌',
          disabled:true,
          fieldName: 'powerMachineBrand',
          placeholder: '电机品牌'
        },
        {
          type: 'INPUT',
          label: '驱动形式',
          disabled:true,
          fieldName: 'driveStyleName',
          placeholder: '驱动形式'
        },
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'isValid',
          disabled:true,
          placeholder: '是否有效',
          itemList: this.createDictList('1004')
        },
        // {
        //   type: 'SELECT',
        //   label: '数据来源',
        //   fieldName: 'dataSources',
        //   placeholder: '数据来源',
        //   disabled:true,
        //   itemList: this.createDictList('1045')
        // },


      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '分类代码',
          // dataIndex: 'baseCode',
          dataIndex: 'basedataType',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '分类名称',
          // dataIndex: 'baseName',
          dataIndex: 'basedataName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
      ];
    },
    init() {
     if(this.type == 'show'){
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
        this.formList.forEach(x => {
          x.placeholder = "";
        });

        if(!this.formList.find(x=>x.fieldName == "vehiclePrice").initialValue){
          this.formList.find(x=>x.fieldName == "vehiclePrice").initialValue=this.formList.find(x=>x.fieldName == "oemDirectivePrice").initialValue
        }
      }else if(this.type == 'edit'){
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
        if(!this.formList.find(x=>x.fieldName == "vehiclePrice").initialValue){
          this.formList.find(x=>x.fieldName == "vehiclePrice").initialValue=this.formList.find(x=>x.fieldName == "oemDirectivePrice").initialValue
        }

        console.log(this.type,"this.type123")
        this.formList.find(x=>x.fieldName == "baseCode").disabled = false
        this.formList.find(x=>x.fieldName == "maxReserve").disabled = false
        this.formList.find(x=>x.fieldName == "minReserve").disabled = false
        this.formList.find(x=>x.fieldName == "vehicleMarkupRate").disabled = false
        this.formList.find(x=>x.fieldName == "salesBasePrice").disabled = false
        this.formList.find(x=>x.fieldName == "vehiclePrice").disabled = false

        /*
        "vehiclePrice"销售指导价
        "purchasePrice"采购单价
        "vehicleMarkupRate"整车加价率
        */
        let purchasePriceData = this.formList.find(x=>x.fieldName == "purchasePrice").initialValue
        let vehicleMarkupRateData = this.formList.find(x=>x.fieldName == "vehicleMarkupRate").initialValue || 0
        if(purchasePriceData > 0 && 100 >= vehicleMarkupRateData && vehicleMarkupRateData > 0){
          this.formList.find(x=>x.fieldName == "vehiclePrice").initialValue=Math.round(purchasePriceData * (1+vehicleMarkupRateData/100)*100)/100
        }
      }
        // this.formList.find(x=>x.fieldName == "colorStr").initialValue = this.rowData.color
        // this.formList.find(x=>x.fieldName == "interiorColorStr").initialValue = this.rowData.trimColor
    },
    // 搜索
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
      console.log(this.fetchParams)
    },
    //重置
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    openDriveCancel(){
      this.isShow = true
    },
    //数据选中
    onRowSelectChange(val){
      console.log(val,"val")
      if(val && val.length == 1){
        this.tableData = val
      }
      // else{
      //   this.tableData =[]
      //   this.$message(`只能选择一个`);
      // }
    },
    //确定
    addHandel(){
      console.log(this.tableData,"this.tableData")
      if(this.tableData.length == 1){
          this.isShow =false
          // this.formList.forEach(x => {
          //   x.initialValue = this.tableData[0][x.fieldName];
          // });
          this.formList.find(x=>x.fieldName == "baseCode").initialValue = this.tableData[0].basedataType
          this.formList.find(x=>x.fieldName == "baseName").initialValue = this.tableData[0].basedataName
      }
    },
    cancle(){
      this.isShow =false
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog(){
      this.$refs.formPanel.SUBMIT_FORM();
      if(this.saveData !== null){
        let baseId = this.tableData[0] ? this.tableData[0].basedataId:this.rowData.baseId;
        let data = {
          productId: this.productId,
          vehiclePrice: this.saveData.vehiclePrice,
          baseId: baseId
        }
        let res = await editCarPrice({...this.saveData, ...data});

        if (res.resultCode === 200) {
            this.closeDialog('refresh');
          }else{
            this.$message(`${res.errMsg}`);
          }
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    }
  }
};
</script>

<style lang="less" scoped>
</style>
